/**

 * @Title: RegionDaoImpl.java

 * @Package com.yellowpage.ca.dao

 * @Description: TODO

 * Copyright: Copyright (c) 2011 

 * Company:  北京亚信科技有限公司

 * 

 * @author Comsys-xuys

 * @date 2015-6-26 下午3:36:42

 * @version V1.0

 */



package com.yellowpage.ca.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.yellowpage.ca.data.RoleFunction;
import com.yellowpage.white.dao.BaseDaoHibernateImpl;
import com.yellowpage.white.dao.QueryAssistantSQL;



/**

 * @ClassName: RegionDaoImpl

 * @Description: TODO

 * @author Comsys-xuys

 * @date 2015-6-26 下午3:36:42

 *


 */

public class RegionDaoImpl extends BaseDaoHibernateImpl<RoleFunction, Long> implements RegionDao {

	/*
	  * <p>Title: getRegionListByUserFunction</p>
	  * <p>Description:根据用户id及功能获得地区信息 </p>
	  * @param id
	  * @param function
	  * @return
	  * @see com.yellowpage.ca.dao.RegionDao#getRegionListByUserFunction(java.lang.Long, java.lang.String)
	  */
	
	@Override
	public List<String> getRegionListByUserFunction(Long user_id, String function_code) {
		List<String> regionList = new ArrayList<String>();
		String sql = "select ufg.value from CA_USER_FUNCTION uf INNER JOIN CA_USER_FUNCTION_CONFIG ufg " +
				"where uf.user_function_id = ufg.user_function_id and code = 'REGION'" +
				" and uf.user_id = "+user_id+" and uf.function_id = (SELECT FUNCTION_ID FROM CA_FUNCTION WHERE FUNC_CODE = '"+function_code+"' )";
		QueryAssistantSQL sqlAssistant = newQueryAssistantSQL(sql);
		List<Map> resultList = sqlAssistant.query(getConnection(), null);
		for (Map map : resultList) {
			regionList.add((String) map.get("value"));
		}
		return regionList;
	}

	/*
	
	  * <p>Title: getRegionclauseByUserFunction</p>
	
	  * <p>Description: 根据用户id，权限功能，字段获取sql</p>
	
	  * @param id
	  * @param function
	  * @param regionField
	  * @return
	
	  * @see com.yellowpage.ca.dao.RegionDao#getRegionclauseByUserFunction(java.lang.Long, java.lang.String, java.lang.String)
	
	  */
	
	@Override
	public List<String> getRegionclauseByUserFunction(Long user_id, Long function_id,String regionField) {
		List<String> RegionclauseList = new ArrayList<String>();
		String sql = "select ufg."+regionField+" from CA_USER_FUNCTION uf INNER JOIN CA_USER_FUNCTION_CONFIG ufg " +
				"where uf.user_function_id = ufg.user_function_id and code = 'REGION'" +
				" and uf.user_id = "+user_id+" and uf.function_id = "+function_id;
		QueryAssistantSQL sqlAssistant = newQueryAssistantSQL(sql);
		List<Map> resultList = sqlAssistant.query(getConnection(), null);
		for (Map map : resultList) {
			RegionclauseList.add(String.valueOf(map.get(regionField)) );
		}
		return RegionclauseList;
	}

}
